﻿import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:vischat/core/controller/global_service.dart';
import 'package:vischat/core/utils/app_toast.dart';
import 'package:vischat/core/utils/jverify.dart';
import 'package:vischat/data/repositories/auth_repository.dart';
import 'package:vischat/generated/route_table.dart';

class VerifysmsController extends GetxController {
  final GlobalService global;
  final AuthRepository _repo;
  VerifysmsController(this.global, this._repo);

  late final String phone;
  final _pinController = TextEditingController();

  GlobalKey get formKey => GlobalKey<FormState>();
  TextEditingController get pinController => _pinController;

  @override
  void onInit() {
    phone = Get.arguments['phone'];
    global.startCountdown();
    super.onInit();
  }

  Future<void> handSmsLogin(String val) async {
    if (pinController.text.isEmpty) {
      AppToast.showToast('toast.verificationCode'.tr);
      return;
    }
    // final loginToken = await JverifyUtil.getToken();
    await _repo.login(loginType: LoginType.sms, loginToken: '');
    Get.offAllNamed(RouteTable.HOME);
  }

  @override
  void onClose() {
    pinController.dispose();
    global.destroyTimer();
    super.onClose();
  }
}
